<template>
  <div>
      <el-row style="background-color: white;">
          <el-col>
            <el-button type="primary" class="back" @click="$router.back(-1)">返回</el-button>
          </el-col>
          <el-col style="margin: 0px 10%; width: 80%;">
            <p style="text-align: center; font-size: 30px;">高校党委理论学习中心组</p>
            <el-form :model="form" :rules="rules" ref="form" style="font-weight: 600;">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="学校" prop="school">
                          <el-input style="width: 70%" v-model="form.sch_name" disabled></el-input>
                          <!-- <el-select style="width: 80%" v-model="form.sch_name" placeholder="选择或输入学校" clearable filterable>
                            <el-option v-for="item in schools" :key="item.sch_id" :label="item.sch_name" :value="item.sch_name"></el-option>
                          </el-select> -->
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="学习时间" prop="learn_time">
                          <el-date-picker
                            v-model="form.learn_time"
                            type="daterange"
                            style="width: 70%"
                            unlink-panels
                            format="yyyy-MM-dd"
                            value-format="yyyy-MM-dd"
                            @input="changeVal($event)"
                            range-separator=" 至 "
                            start-placeholder="开始日期"
                            end-placeholder="结束日期">
                          </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item label="学习次数" required>
                        <el-form-item prop="learn_year" style="display: inline-block">
                            <el-input
                              v-model="form.learn_year"
                              class="xiahuaxian"
                            >
                            </el-input>
                            年度第
                            </el-form-item>
                            <el-form-item prop="learn_num" style="display: inline-block">
                              <el-input
                              v-model="form.learn_num"
                              class="xiahuaxian"
                            >
                            </el-input>
                            次中心组学习
                            </el-form-item>
                      </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="学习内容" prop="learn_content">
                            <el-input
                              type="textarea"
                              placeholder="请输入内容(500字以内)"
                              v-model="form.learn_content"
                              :maxlength='500'
                              show-word-limit
                              style="width: 85%"
                            >
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="学习形式（多选）" prop="learn_ways">
                            <el-checkbox-group v-model="form.learn_ways" @change="changeOther1">
                              <el-checkbox :label="0">集体学习研讨</el-checkbox>
                              <el-checkbox :label="1">个人自学</el-checkbox>
                              <el-checkbox :label="2">专题调研</el-checkbox>
                              <el-checkbox :label="3">扩大会</el-checkbox>
                              <el-checkbox :label="4">其他</el-checkbox>
                              <el-form-item prop="other1" style="display: inline-block">
                              <el-input :disabled="disabled1" class="xiahuaxian" v-model="form.other1"></el-input>
                              </el-form-item>
                            </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="参加人员（多选）" prop="join_person">
                          <el-checkbox-group v-model="form.join_person" @change="changeOther2">
                            <el-checkbox :label="0">校党委中心组成员</el-checkbox>
                            <el-checkbox :label="1">全体校领导</el-checkbox>
                            <el-checkbox :label="2">全体中层以上党员干部</el-checkbox>
                            <el-checkbox :label="3">各单位/部门主要负责人</el-checkbox>
                            <el-checkbox :label="4">副处级及以上干部</el-checkbox>
                            <el-checkbox :label="5">二级党组织书记</el-checkbox>
                            <el-checkbox :label="6">其他</el-checkbox>
                            <el-form-item prop="other2" style="display: inline-block">
                            <el-input :disabled="disabled2" class="xiahuaxian" v-model="form.other2"></el-input>
                            </el-form-item>
                          </el-checkbox-group>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="相关新闻链接" prop="news">
                          <el-input v-model="form.newsurl" placeholder="非必填项超链接" style="width: 85%;"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <p style="color: #606266; font-size: 16px;">报告人非校党委中心组成员的请填写：</p>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="报告人" prop="reporter">
                            <el-input
                              type="textarea"
                              placeholder="请输入报告人(纯文本20字以内)"
                              v-model="form.reporter"
                              :maxlength='20'
                              show-word-limit
                              style="width: 85%"
                            >
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="报告人职务/职称" prop="reporter_career">
                            <el-input
                              type="textarea"
                              placeholder="请输入报告人职务/职称(纯文本30字以内)"
                              v-model="form.reporter_career"
                              :maxlength='30'
                              show-word-limit
                              style="width: 85%"
                            >
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                        <el-form-item label="备注" prop="remark">
                            <el-input
                              type="textarea"
                              placeholder="请输入备注"
                              v-model="form.remark"
                              :maxlength='100'
                              show-word-limit
                              style="width: 85%"
                            >
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <p style="color: #606266; font-size: 16px;"> <span style="color: red;">*</span> 添加附件（限PDF，必填项）：</p>
                    </el-col>
                    <el-col :span="24" class="Label">
                        <el-form-item label="学习方案:" prop="studyPlan">
                           <el-upload
                             class="upload-demo"
                             name="file"
                             :show-file-list='false'
                             :data="{ sch_name: form.sch_name, sch_id: form.sch_id, info_id: this.$route.params.id, file_id2: form.learn_fangan_file_id }"
                             :file-list="fileList2"
                             :on-success="handleSuccess2"
                             action="http://8.142.34.26:8096/php/index.php/admin/Upload/upload2"
                             multiple
                             accept='.PDF'>
                             <span style="font-size: 10px;"> {{form.file2_name}} </span>
                             <el-button size="small" style="margin-left: 20px;">
                                <i class="el-icon-upload"></i>
                                <span>重新上传</span>
                             </el-button>
                             <!-- <div class="el-upload__tip" slot="tip">只能上传pdf文件</div> -->
                           </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" class="Label">
                        <el-form-item label="学习记录:" prop="studyRecord">
                           <el-upload
                             class="upload-demo"
                             name="file"
                             :show-file-list='false'
                             :data="{ sch_name: form.sch_name, sch_id: form.sch_id, info_id: this.$route.params.id, file_id3: form.learn_jilu_file_id }"
                             :file-list="fileList3"
                             :on-success="handleSuccess3"
                             action="http://8.142.34.26:8096/php/index.php/admin/Upload/upload3"
                             multiple
                             accept='.PDF'>
                             <span style="font-size: 10px;"> {{form.file3_name}} </span>
                             <el-button size="small" style="margin-left: 20px;">
                                <i class="el-icon-upload"></i>
                                <span>重新上传</span>
                             </el-button>
                             <!-- <div class="el-upload__tip" slot="tip">只能上传pdf文件</div> -->
                           </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" class="Label">
                        <el-form-item label="学习报告:" prop="studyReport">
                           <el-upload
                             class="upload-demo"
                             name="file"
                             :show-file-list='false'
                             :data="{ sch_name: form.sch_name, sch_id: form.sch_id, info_id: this.$route.params.id, file_id1: form.learn_baogao_file_id }"
                             :file-list="fileList1"
                             :on-success="handleSuccess1"
                             action="http://8.142.34.26:8096/php/index.php/admin/Upload/reload1"
                             multiple
                             accept='.PDF'
                             >
                             <span style="font-size: 10px;"> {{form.file1_name}} </span>
                             <el-button size="small" style="margin-left: 20px;">
                                <i class="el-icon-upload"></i>
                                <span>重新上传</span>
                             </el-button>
                             <!-- <div class="el-upload__tip" slot="tip">只能上传pdf文件</div> -->
                           </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24" class="Label">
                        <el-form-item label="其他材料:" prop="otherFile">
                           <el-upload
                             class="upload-demo"
                             name="file"
                             :show-file-list='false'
                             :data="{ sch_name: form.sch_name, sch_id: form.sch_id, info_id: this.$route.params.id, file_id4: form.learn_other_file_id }"
                             :file-list="fileList4"
                             :on-success="handleSuccess4"
                             action="http://8.142.34.26:8096/php/index.php/admin/Upload/reload4"
                             multiple
                             accept='.PDF'
                             >
                             <span style="font-size: 10px;"> {{form.file4_name}} </span>
                             <el-button size="small" style="margin-left: 20px;">
                                <i class="el-icon-upload"></i>
                                <span>重新上传</span>
                             </el-button>
                             <!-- <div class="el-upload__tip" slot="tip">只能上传pdf文件</div> -->
                           </el-upload>
                        </el-form-item>
                    </el-col>
                    <el-col :span="24">
                      <el-form-item style="text-align: center; margin: 20px 0px;">
                          <el-button type="primary" @click="submitForm('form')">提交</el-button>
                          <!-- <el-button type="warning" @click="resetForm('form')">重置</el-button> -->
                      </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
          </el-col>
      </el-row>
  </div>
</template>

<script>
import http from '../../../assets/js/http'
export default {
  data() {
    return {
      form: {
        join_person: [],
        learn_content: '',
        learn_time: [],
        learn_ways: [],
        newsurl: '',
        sch_name: '',
        sch_id: '',
        other1: '',
        other2: ''
      },
      schools: [],
      fileList1: [],
      fileList2: [],
      fileList3: [],
      mode: {},
      disabled1: '',
      disabled2: '',
      rules: {
        learn_time: [
          { required: true, message: '请选择学习时间', trigger: 'blur' }
        ],
        learn_year: [
          { required: true, message: '请选择学习年度', trigger: 'blur' }
        ],
        learn_num: [
          { required: true, message: '请选择学习次数', trigger: 'blur' }
        ],
        learn_content: [
          { required: true, message: '请选择学习内容', trigger: 'blur' }
        ],
        learn_ways: [
          { required: true, message: '请选择学习形式', trigger: 'blur' }
        ],
        join_person: [
          { required: true, message: '请选择学习参加人员', trigger: 'blur' }
        ]
        // reporter: [
        //   { required: true, message: '请选择学习报告人', trigger: 'blur' }
        // ],
        // reporter_career: [
        //   { required: true, message: '请选择学习报告人职称', trigger: 'blur' }
        // ]
      }
    }
  },
  activated() {},
  watch: {},
  created() {
    this.init()
  },
  mounted() {},
  methods: {
    init () {
      this.getInfoById()
    },
    changeOther1 (val) {
      console.log(val)
      if (val.length !== 0) {
        for (var i = 0; i < val.length; i++) {
          if (val[i] === 4) {
            this.disabled1 = false
            return
          } else {
            this.disabled1 = true
          }
        }
      } else {
        this.disabled1 = true
      }
    },
    changeOther2 (val) {
      console.log(val)
      if (val.length !== 0) {
        for (var i = 0; i < val.length; i++) {
          if (val[i] === 6) {
            this.disabled2 = false
            return
          } else {
            this.disabled2 = true
          }
        }
      } else {
        this.disabled2 = true
      }
    },
    getInfoById () {
      const data = {
        id: this.$route.params.id
      }
      this.apiPost('admin/Info/infoDetail', data).then((res) => {
        this.handelResponse(res, (data) => {
          console.log(res.data)
          this.form = res.data
          if (this.form.join_person[0] !== '') {
            this.form.join_person = this.form.join_person.map(Number)
          }
          if (this.form.learn_ways[0] !== '') {
            this.form.learn_ways = this.form.learn_ways.map(Number)
          }
          for (var j = 0; j < this.form.join_person.length; j++) {
            if (this.form.join_person[j] === 6) {
              this.disabled2 = false
            } else {
              this.disabled2 = true
            }
          }
          for (var i = 0; i < this.form.learn_ways.length; i++) {
            if (this.form.learn_ways[i] === 4) {
              this.disabled1 = false
            } else {
              this.disabled1 = true
            }
          }
          this.form.learn_time = [res.data.learn_start_time, res.data.learn_end_time]
          console.log(this.form)
        //   this.form = res.data
        //   console.log(this.form)
        })
      })
    },
    handleSuccess1 (response, file, filelist) {
      console.log(file)
      if (response.code === 200) {
        this.form.file1_name = file.name
        this.form.learn_baogao_file_id = response.data.learn_baogao_file_id
        // console.log(this.form.file_id1)
      } else {
        this.$message({
          type: 'warning',
          message: '提交失败!'
        })
      }
    },
    handleSuccess2 (response, file, filelist) {
      console.log(file)
      if (response.code === 200) {
        this.form.file2_name = file.name
        this.form.learn_fangan_file_id = response.data.learn_fangan_file_id
        // console.log(this.form.file_id1)
      } else {
        this.$message({
          type: 'warning',
          message: '提交失败!'
        })
      }
    },
    handleSuccess3 (response, file, filelist) {
      console.log(file)
      if (response.code === 200) {
        this.form.file3_name = file.name
        this.form.learn_jilu_file_id = response.data.learn_jilu_file_id
        // console.log(this.form.file_id1)
      } else {
        this.$message({
          type: 'warning',
          message: '提交失败!'
        })
      }
    },
    handleSuccess4 (response, file, filelist) {
      console.log(file)
      if (response.code === 200) {
        this.form.file4_name = file.name
        this.form.learn_other_file_id = response.data.learn_other_file_id
        // console.log(this.form.file_id1)
      } else {
        this.$message({
          type: 'warning',
          message: '提交失败!'
        })
      }
    },
    changeVal (val) {
      // console.log(val)
      if (val !== null) {
        for (var i = 0; i < 2; i++) {
          val[i] = moment(val[i]).format('YYYY-MM-DD')
        }
        this.$nextTick(() => {
          this.form.learn_time = null
          this.$set(this.form, 'learn_time', [val[0], val[1]])
        })
        this.rules.learn_time = []
      } else {
        this.rules.learn_time = [
          { required: true, message: '请选择学习时间', trigger: 'blur' }
        ]
      }
      this.$forceUpdate()
      console.log(this.form.learn_time)
    },
    submitForm (form) {
      if (this.form.learn_time !== null) {
        for (var i = 0; i < 2; i++) {
          this.form.learn_time[i] = moment(this.form.learn_time[i]).format('YYYY-MM-DD')
        }
      }
      // this.form.datetime = moment(this.form.datetime).format('YYYY-MM-DD HH:mm:ss')
      console.log(this.form)
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.$confirm('是否确认提交？', '核对信息', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            center: true
          }).then(() => {
            this.apiPost('admin/Info/updateById', this.form).then((res) => {
              this.handelResponse(res, (data) => {
                if (res.code === 200) {
                  this.$message({
                    type: 'success',
                    message: res.data
                  })
                  this.$router.push({ name: 'consultsList' })
                } else {
                  this.$message({
                    type: 'error',
                    message: res.error
                  })
                }
              })
            }).catch(() => {
            })
          }).catch(() => {
            this.$message({
              type: 'info',
              message: '已取消提交'
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  mixins: [http]
}
</script>

<style scoped>
.Label .el-form-item .el-form-item__label {
    width: 115px;
}
.xiahuaxian {
  width: 100px;
  display: inline-block;
}
.xiahuaxian .el-input__inner {
  width: 100px;
  margin-left: 5px;
  border: 0;
  border-bottom: 1px solid #888;
  border-radius: 0;
}
.back {
  position: absolute;
  top: 10px;
  right: 10px;
}
</style>
